//
//  YHKTabBar.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/2/28.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

class YHKTabBar: UITabBar {

//MARK:添加视图的三部曲
    
    //第一部：init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        

        //调用方法
        setUp()
        
        
//        backgroundColor = UIColor(white: 237/255.0, alpha: 1)

        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //第二部：私有的添加视图和设置约束
    private func setUp(){
    
        addSubview(addButton)
    }
    
    //第三部：懒加载
    lazy var addButton:UIButton = {
        
        //1.创建视图
        let btn = UIButton(type: UIButtonType.Custom)
        //2.设置图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //3.按钮大小
        btn.sizeToFit()
        
        
       return btn
        
    }()
    
//MARK:设置tabBar子控件的farm
    
    //设置frame
    override func layoutSubviews() {
        
        var index = 0
        let W = bounds.size.width / 5
        let H = bounds.size.height
        
        super.layoutSubviews()
        
        //1.首先要拿到tabBar中的所有子控件
        for subview in subviews{
            
            //2.判断tabBar中的控件是不是UITabBarButton这个类型
                 //2.1将字符串转为类
            if subview.isKindOfClass( NSClassFromString("UITabBarButton")!){
            
            //3.依次设置farme
            subview.frame = CGRectMake(CGFloat (index++) * W, 0, W, H)
                
                  //空出中的位置来添加btn
                  if index == 2{index++}
                
            }
            }
        
        addButton.frame = CGRectMake(2 * W, 0, W, H)
    }
    
    
    

}
